
INPUT_FILE = "./inventory.list"
OUTPUT_FILE = "./inventory.ini"


def main():
    
    input_text = None
    with open(INPUT_FILE, 'r') as f:
        input_text = f.readlines()
    
    inventory = {}
    for entry in input_text:
        
        (host, ip) = entry.split(":")
        (group, num) = host.split("-")
        ip = ip[:-1]
        
        if group not in inventory:
            inventory[group] = []
        inventory[group].append({ "host": host, "ip": ip })
    
    with open(OUTPUT_FILE, "w") as f:
        
        for group in inventory:
            f.writelines([f"\n[{group}]\n"])
            for entry in inventory[group]:
                f.writelines([f"{entry['host']} ansible_host={entry['ip']}\n"])
            
        f.writelines([
            "\n[java-apps:children]\n",
            "gw\n",
            "srv\n"
        ])


if __name__ == "__main__":
    main()
    print("done")
